package com.freecloud.javabasics.design.single;

import com.freecloud.javabasics.design.entity.car.Car;

/**
 * 单例安全，防止反序列化或反射方式破坏单例
 * @Author: maomao
 * @Date: 2020-02-29 21:43
 */
public class SingleSafety {

    private static Car car = null;

    /**
     * 修改默认无参数构造函数为私有，并在内部增加锁判断校验
     */
    private SingleSafety(){
        //方法1： 解决反射破坏
        synchronized (Lazy.class){
            System.out.println("synchronized Lazy()");
        }
        //方法2： 判断静态实例，防止反射破坏
        if(car != null){
            throw new IllegalStateException();
        }
    }
}
